package com.example.seh.person;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.seh.R;
import com.example.seh.constant.Constant;
import com.example.seh.person.buy.BoughtActivity;
import com.example.seh.person.post.MyPostActivity;
import com.example.seh.person.publish.MyPublishActivity;
import com.example.seh.person.sell.SellActivity;
import com.example.seh.util.ImgUtil;
import com.example.seh.util.LoginActivity;
import com.example.seh.util.LoginUtil;
import com.example.seh.util.UserMsg;

import de.hdodenhof.circleimageview.CircleImageView;


public class MeFragment extends Fragment {
    private CardView cardView;
    private CircleImageView avatar;
    private ImageView personImg;
    private TextView tvName, tvAccount, login;
    private LinearLayout lyName, lyAccount;
    private LinearLayout exitLogin;
    private TextView personSetting;
    private RelativeLayout buy, sell, publish, text;
    private boolean isFirstLoading = true;
    private int backNum = 1;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person, null);
        System.out.println("mefragment执行");
        getViews(view);
        setListeners();

        getDatas();
        return view;
    }

    private void getDatas() {
        if (Constant.isLogin) {
            login.setVisibility(View.INVISIBLE);
            lyName.setVisibility(View.VISIBLE);
            lyAccount.setVisibility(View.VISIBLE);
            exitLogin.setVisibility(View.VISIBLE);
            try {
                if (UserMsg.userDTO != null) {
                    if (!"".equals(UserMsg.userDTO.getPic())) {

//                        avatar.setImageBitmap(ImgUtil.toRoundBitmap(ImgUtil.stringToBitmap(UserMsg.userDTO.getPic())));

                        //avatar.setImageBitmap(ImgUtil.toRoundBitmap(ImgUtil.stringToBitmap(UserMsg.userDTO.getPic())));
                        Glide.with(getContext())
                                .load(UserMsg.userDTO.getPic())
                                .diskCacheStrategy(DiskCacheStrategy.NONE)
                                .into(avatar);
                    }
                    tvName.setText(UserMsg.userDTO.getUsername());
                    tvAccount.setText(UserMsg.userDTO.getPhone());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.default_avatar);
            avatar.setImageBitmap(ImgUtil.toRoundBitmap(mBitmap));
            exitLogin.setVisibility(View.INVISIBLE);
        }

//        Bundle bundle = getArguments();
//        if (bundle != null) {
//            Log.e("update","bundle not null");
//            if ("update".equals(bundle.getString("update"))) {
//                Log.e("update",UserMsg.userDTO.getUsername());
//                tvName.setText(UserMsg.userDTO.getUsername());
//            }
//        }

    }

    private void setListeners() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivityForResult(intent,100);

            }
        });
        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin) {
                    Constant.isLogin = false;
                    login.setVisibility(View.VISIBLE);
                    lyName.setVisibility(View.INVISIBLE);
                    lyAccount.setVisibility(View.INVISIBLE);
                    Toast.makeText(getContext(), "退出登录成功", Toast.LENGTH_SHORT).show();
//                    exitListener.edit(true);
//                    Intent intent = new Intent(getActivity(), MainActivity.class);
//                    intent.putExtra("exit", true);
//                    startActivityForResult(intent, 200);
                    ListenerManager listenerManager = new ListenerManager(getContext());
                    listenerManager.getInstance(getActivity()).updata();
                }
            }
        });
        personSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin) {
                    Intent intent = new Intent(getContext(), PersonSettingActivity.class);
                    startActivityForResult(intent,300);
                } else {
                    LoginUtil.toLogin(getContext());
                }

            }
        });
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin){
                    Intent intent = new Intent(getContext(), BoughtActivity.class);
                    startActivity(intent);
                }else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(),"请登录后查看",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
        sell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin){
                    Intent intent = new Intent(getContext(), SellActivity.class);
                    startActivity(intent);
                }else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(),"请登录后查看",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

        publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin){
                    Intent intent = new Intent(getContext(), MyPublishActivity.class);
                    startActivity(intent);
                }else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(),"请登录后查看",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });

        text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Constant.isLogin){
                    Intent intent = new Intent(getContext(), MyPostActivity.class);
                    startActivity(intent);
                }else {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getContext(),"请登录后查看",Toast.LENGTH_SHORT).show();
                        }
                    });
                }

            }
        });
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (backNum == 1) {
                    personImg.setBackgroundResource(0);
                    personImg.setBackgroundResource(R.mipmap.person4);
                }
                if (backNum == 2) {
                    personImg.setBackgroundResource(0);
                    personImg.setBackgroundResource(R.mipmap.person5);
                }
                if (backNum == 3) {
                    personImg.setBackgroundResource(0);
                    personImg.setBackgroundResource(R.mipmap.person6);
                }
                if (backNum == 4) {
                    personImg.setBackgroundResource(0);
                    personImg.setBackgroundResource(R.mipmap.person3);
                }
                backNum++;
                if (backNum == 5) {
                    backNum = 1;
                }
            }
        });
    }

    private void getViews(View view) {
        cardView = view.findViewById(R.id.person_setting_back);
        personImg = view.findViewById(R.id.person_background);
        avatar = view.findViewById(R.id.person_avatar);
        tvName = view.findViewById(R.id.person_name);
        tvAccount = view.findViewById(R.id.person_account);
        login = view.findViewById(R.id.login);
        lyName = view.findViewById(R.id.layout_name);
        lyAccount = view.findViewById(R.id.layout_account);
        exitLogin = view.findViewById(R.id.exit_login);
        personSetting = view.findViewById(R.id.person_setting);

        buy = view.findViewById(R.id.gwc_back);
        sell = view.findViewById(R.id.qb_back);
        publish = view.findViewById(R.id.sj_back);
        text = view.findViewById(R.id.tb_back);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!isFirstLoading && UserMsg.userDTO != null) {
            Log.e("update","刷新");
//            try {
//
////                if (UserMsg.userDTO.getPic() != null) {
////                    avatar.setImageBitmap(ImgUtil.toRoundBitmap(ImgUtil.stringToBitmap(UserMsg.userDTO.getPic())));
//
////                if (UserMsg.userDTO.getPic()!=null) {
////                    avatar.setImageBitmap(ImgUtil.toRoundBitmap(ImgUtil.stringToBitmap(UserMsg.userDTO.getPic())));
////                }
//                if (UserMsg.userDTO.getPic()!=null){
//                    Glide.with(getContext())
//                            .load(UserMsg.userDTO.getPic())
//                            .diskCacheStrategy(DiskCacheStrategy.NONE)
//                            .into(avatar);
//
//                }
//                tvName.setText(UserMsg.userDTO.getUsername());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
            getDatas();
        }
        isFirstLoading = false;
    }
}
